package com.bj58.ecdata.mailreport.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.mailreport.db.CommonDao;
import com.bj58.ecdata.mailreport.db.DBConfig;
import com.bj58.ecdata.mailreport.model.ErShouXiaoshou;
import com.bj58.ecdata.mailreport.model.JianliReportKpi;
import com.bj58.ecdata.mailreport.model.TaskLog;
import com.bj58.ecdata.mailreport.service.EmailReportService;
import com.bj58.ecdata.mailreport.threads.IESendTimerTask;
import com.bj58.ecdata.mailreport.util.DateUtil;
import com.bj58.ecdata.mailreport.util.MyPathUtil;
import com.bj58.ecdata.mailreport.util.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.template.TemplateMethodFactory;
import com.bj58.groupbuy.util.PathUtil;


/**自定义邮件系统 - 附加代码部分
 * 方法不要捕获异常，异常由自定义邮件部分处理。
 * @author huangliang
 *
 */
public class IntEmailAddController {
	private EmailReportService emailReportService = ServiceLocator.get(EmailReportService.class);
	private CommonDao comDao = new CommonDao();
	
	@SuppressWarnings("unused")
	private String checkSrcTable(String dbHost,String dbName,String tableName,String dateFiled,String searchDate,DBConfig dbConfig)throws Exception{
		//年月日
		String day = searchDate.replace("-", ""); //yyyyMMdd
		String month = searchDate.substring(0,searchDate.lastIndexOf("-")).replace("-", ""); ///yyyyMM
		String year =  searchDate.substring(0,searchDate.indexOf("-")); ///yyyy
		
		String sql = "select run_date,db_name,tb_name,exe_type,status,result_count from task.v_task where run_date='%s' and db_host='%s' and tb_name='%s'";
		
		String lastDay = DateUtil.strToStrBefore(searchDate,"yyyy-MM-dd",1);
		String lastWeek = DateUtil.strToStrBefore(searchDate,"yyyy-MM-dd",7);
		String dataArr[] = {searchDate,lastDay,lastWeek};
		IESendTimerTask.getLogger().info("begin check table "+dbName+"."+tableName);

		for(String date : dataArr){
				boolean hasData = false;
				int dbID = dbConfig.getIntType();
				String status = null;
				//查任务调度执行结果日志
				List<TaskLog> runLog = comDao.getAllColsByVo(String.format(sql,date,dbHost,tableName), TaskLog.class, DBConfig.db_manage.getIntType());
				if(runLog == null || runLog.isEmpty()){
					//没有日志，直接查库
					if(StringUtils.isBlank(dateFiled))
						dateFiled = "stat_date";
					String sql2 = "select count(1) from " + tableName.replace("#Ymd#", day).replace("#Ym#", month).replace("#Y#", year)+" where "+dateFiled+"='"+searchDate+"'";
					int count = comDao.countAll(sql2, dbID);
					if(count>0)
						hasData = true;
				}else{
					TaskLog taskLog = runLog.get(0);
					status = taskLog.getStatus();
					if("success".equals(status))
						hasData = true;
				}
				
				if(hasData == false)
					return dbName+"."+tableName+" : "+status; 
			}
		return "true";
	}

	
	
	/**简历日报--附加数据
	 * 
	 */
	public String jianliReport(String searchDate,String filepath)throws Exception{
		//velocity对象
		Map<String,Object> context = new HashMap<String,Object>();
		//读取模板内容
		String content = MyPathUtil.getTemByPath(filepath);
		Object tools = TemplateMethodFactory.getInstance(VelocityTools.class);
		context.put("tools",tools);
		
		Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
		String nowDate = DateUtil.getStringDate(sDate,"yyyy-MM-dd");
		String lastDate = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");
		String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");
		context.put("nowDate",nowDate.replace("-", "."));
		context.put("lastDate",lastDate.replace("-", "."));
		context.put("lastWeek",lastWeek.replace("-", "."));
		
		List<String> dateList = new ArrayList<String>();
		dateList.add(nowDate);dateList.add(lastDate);dateList.add(lastWeek);
		
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("nowDate",nowDate);
		dateMap.put("lastDate", lastDate);
		dateMap.put("lastWeek",lastWeek);
		
		//简历月度kpi数据
		context.put("kpiDate",nowDate);
		JianliReportKpi jlReportKpi = emailReportService.getJLKpiData(nowDate);
		context.put("jlReportKpi",jlReportKpi);
		content = VelocityTools.render(content, context);
		return content;
	}

	/**二手销售日报(张鑫龙)--附加数据
	 * 
	 */
	public String ershouXiaoshou(String searchDate,String filepath)throws Exception{
		//velocity对象
		Map<String,Object> context = new HashMap<String,Object>();
		//读取模板内容
		String content = MyPathUtil.getTemByPath(filepath);
		Object tools = TemplateMethodFactory.getInstance(VelocityTools.class);
		context.put("tools",tools);
		
		Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
		String nowDate = DateUtil.getStringDate(sDate,"yyyy-MM-dd");
		String lastDate = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 1),"yyyy-MM-dd");
		String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");
		context.put("nowDate",nowDate);
		context.put("lastDate",lastDate);
		context.put("lastWeek",lastWeek);
		
		List<String> dateList = new ArrayList<String>();
		dateList.add(nowDate);dateList.add(lastDate);dateList.add(lastWeek);
		
		Map<String,String> dateMap = new HashMap<String, String>();
		dateMap.put("nowDate",nowDate);
		dateMap.put("lastDate", lastDate);
		dateMap.put("lastWeek",lastWeek);
		
		List<String> days = DateUtil.getDateList(DateUtil.firstDayMonthBefore(nowDate, "yyyy-MM-dd", 0), nowDate);
//		int monthDays = DateUtil.getMonthDays(nowDate,"yyyy-MM-dd"); //当月总天数
//		int dayOfMonth =  DateUtil.getDayOfMonth(nowDate,"yyyy-MM-dd");
//		double timeProcess = dayOfMonth*1.0/dayOfMonth;
		
		//二手产品线销售     "逐日"   现金回款/消耗报表
		Map<String, ErShouXiaoshou> dayData = emailReportService.getErshouXSDayData(nowDate);
		
		//二手产品线销售 现金回款/消耗报表
		dayData = emailReportService.getErshouXSRefoundData(nowDate,dayData);
		String keySuffixList[] = {"新签","续费","充值","回款总计","精准","置顶","智能","消耗总计"};
		context.put("xsDayData",dayData);
		context.put("days",days);
		context.put("keySuffixList",keySuffixList);
		content = VelocityTools.render(content, context);
		return content;
	}
//	
//	/**二手客服必打电话(张鑫龙)--附加数据
//	 * 
//	 */
//	public String ershouBDDH(String searchDate,String filepath)throws Exception{
//		//velocity对象
//		Map<String,Object> context = new HashMap<String,Object>();
//		//读取模板内容
//		String content = MyPathUtil.getTemByPath(filepath);
//		Object tools = TemplateMethodFactory.getInstance(VelocityTools.class);
//		context.put("tools",tools);
//		context.put("nowDate",searchDate);
//		
//		//二手产品线销售     "逐日"   现金回款/消耗报表
//		Map<String, ErShouXiaoshou> dayData = emailReportService.getErshouBDDHData(searchDate);
//		
//		//二手产品线销售 现金回款/消耗报表
//		dayData = emailReportService.getErshouXSRefoundData(nowDate,dayData);
//		context.put("keySuffixList",keySuffixList);
//		content = VelocityTools.render(content, context);
//		return content;
//	}
	
	public static void main(String[] args) throws Exception{
		IntEmailAddController controller = new IntEmailAddController();
		String filepath =PathUtil.getWFNamespaceFolder()+"/intEmail/ershouXS.html";
		controller.ershouXiaoshou("2015-04-15",filepath);
	}
}
